package com.microcore.zeromq.test;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.UUID;

/**
 * @author FengJie
 * @date 2018/6/2
 */
@RunWith(SpringRunner.class)
@Slf4j
public class GeneratePackage {

    @Test
    public void getPackage() {
        resolve(getByte());
    }

    //@Test
    public void resolve(byte[] data) {

        byte[] headAndTail = int2byte(1234567890);

        byte[] head = Arrays.copyOfRange(data, 0, 4);
        byte[] tail = Arrays.copyOfRange(data, data.length - 4, data.length);
        //判断首尾是否相等
        if (Arrays.equals(headAndTail, head)) {
            if (Arrays.equals(headAndTail, tail)) {

                for (int i = 0; i < 401; i += 100) {
                    if (data[i + 0 + 4] != 0x01) {
                        log.error("第" + i + 0 + 4 + "个字节数据丢包: 现在数据为: " + data[i + 0 + 4]);
                    }
                    if (data[i + 1 + 4] != 0x02) {
                        log.error("第" + i + 1 + 4 + "个字节数据丢包: 现在数据为: " + data[i + 1 + 4]);
                    }
                    if (data[i + 2 + 4] != 0x04) {
                        log.error("第" + i + 2 + 4 + "个字节数据丢包: 现在数据为: " + data[i + 2 + 4]);
                    }
                    if (data[i + 3 + 4] != 0x08) {
                        log.error("第" + i + 3 + 4 + "个字节数据丢包: 现在数据为: " + data[i + 3 + 4]);
                    }
                    if (data[i + 4 + 4] != 0x10) {
                        log.error("第" + i + 4 + 4 + "个字节数据丢包: 现在数据为: " + data[i + 4 + 4]);
                    }

                }
            } else {
                log.error("帧尾丢包: 现在数据为: " + byte2int(tail));
            }
        } else {
            log.error("帧头丢包: 现在数据为: " + byte2int(head));
        }
    }

    byte[] getByte() {
        byte[] data = new byte[640 * 32 + 8];
        String str = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        byte[] c = str.getBytes();

        for (int i = 0; i < 640; i++) {
            for (int j = 0; j < 32; j++) {
                data[i * 32 + j + 4] = c[j];
            }
        }

        for (int i = 0; i < 401; i += 100) {
            data[i + 0 + 4] = 0x01;
            data[i + 1 + 4] = 0x02;
            data[i + 2 + 4] = 0x04;
            data[i + 3 + 4] = 0x08;
            data[i + 4 + 4] = 0x10;
        }

        byte[] headAndTail = int2byte(1234567890);
        //变量依次是(原数组，从原数组第几位开始，目标数组，从第几位开始粘贴，复制长度)
        System.arraycopy(headAndTail, 0, data, 0, headAndTail.length);
        System.arraycopy(headAndTail, 0, data, data.length - headAndTail.length, headAndTail.length);
        for (int i = 0; i < data.length; i++) {
            System.out.print("第" + i + "位:" + data[i] + "  ");
            if (i % 100 == 0) {
                System.out.println();
            }
        }
        return data;

    }

    byte[] int2byte(int res) {
        byte[] targets = new byte[4];

        targets[0] = (byte) (res & 0xff);// 最低位
        targets[1] = (byte) ((res >> 8) & 0xff);// 次低位
        targets[2] = (byte) ((res >> 16) & 0xff);// 次高位
        targets[3] = (byte) (res >>> 24);// 最高位,无符号右移。
        return targets;
    }

    int byte2int(byte[] res) {
// 一个byte数据左移24位变成0x??000000，再右移8位变成0x00??0000
        int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00) // | 表示安位或
                | ((res[2] << 24) >>> 8) | (res[3] << 24);
        return targets;
    }

}
